package com.chen.controller.dto;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Result;
import com.chen.mapper.UserMapper;
import com.chen.pojo.Order;
import com.chen.pojo.Point;
import com.chen.pojo.User;
import com.chen.service.Impl.PointServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.Date;


/**
 * @author chen
 * @date 2022/5/9 11:28
 */


@RestController
@RequestMapping("/point")
@Api(value = "PointController", tags = "积分")
public class PointController {

    @Autowired
    private HttpServletRequest request;


    @Autowired
    private PointServiceImpl pointService;

    @Autowired
    private UserMapper userMapper;


    @GetMapping("/findAll")
    public Result findAll() {

        return Result.success(pointService.list());
    }

    @GetMapping("/selectOne/{id}")
    public Result selectOne(@PathVariable("id") int id) {
        return Result.success(pointService.selectOne(id));
    }


    @GetMapping("/page")
    @ApiOperation("分页")
    public IPage<Point> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "0") Integer point) {
        IPage<Point> page = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!"".equals(point)) {
            queryWrapper.like("point", point);
        }
        return pointService.page(page, queryWrapper);

    }

    @PutMapping("/updatePoint")
    @ApiOperation("更新")
    public Result updatePoint(@RequestBody Point point){
        return Result.success(pointService.updateById(point));
    }
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除")
    public Result delete(@PathVariable("id") Long id){
        return Result.success(pointService.removeById(id));
    }

    @PostMapping("/addPoint")
    @ApiOperation("添加")
    public Result addPoint(@RequestBody Point point){
        point.setTime(new Date());
        String userId = request.getHeader("X-ID");
        User user = userMapper.selectOne1(Integer.parseInt(userId));
        point.setUsername(user.getUsername());
        point.setName("积分");
        return Result.success(pointService.save(point));
    }

}
